/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package elaborate.h2x.parser;

import elaborate.h2x.transform.DefaultRootImpl;
import elaborate.h2x.transform.Root;
import elaborate.h2x.transform.TransformingModel;
import elaborate.h2x.transform.TransformingNode;
import elaborate.h2x.transform.TransformingNodeId;
import elaborate.h2x.transform.service.Locator;
import elaborate.h2x.transform.service.Services;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;

/**
 *
 * @author lendle
 */
public class DefaultRootFactoryImpl implements RootFactory{

    public void createRoot(Services services, TransformingModel model, TransformingNode node) throws ParserException {
        Element element=(Element) node.getOriginalNode();
        Attr rootAttr = element.getAttributeNodeNS("h2x", "root");
        String rootAttrValue=rootAttr.getValue();
        if(rootAttrValue==null || rootAttrValue.length()==0){
            throw new ParserException("empty root setting");
        }
        String [] roots=rootAttrValue.split(";");
        if(roots==null || roots.length==0){
            throw new ParserException("empty root setting");
        }

        for(String root_exp : roots){
            String [] rootDetail=root_exp.split(",");
            DefaultRootImpl root=new DefaultRootImpl();
            if(rootDetail.length==2){
                this.parse(rootDetail[0], rootDetail[1], services, model, node, root);
            }else if(rootDetail.length==1){
                this.parse(rootDetail[0], services, model, node, root);
            }else{
                throw new ParserException("wrong argument number for root setting: must be 2 or 1");
            }
        }
    }

    protected void parse(String target_exp, String root_exp, Services services, TransformingModel model, TransformingNode node, DefaultRootImpl root){
        Locator locator=services.getLocator();
        this.parse(
                locator.locateNode(model, node, target_exp).getId(),
                locator.locateNode(model, node, root_exp).getId(),
                services,
                model,
                node,
                root);
    }

    protected void parse(String root_exp, Services services, TransformingModel model, TransformingNode node, DefaultRootImpl root){
        Locator locator=services.getLocator();
        this.parse(
                node.getId(),
                locator.locateNode(model, node, root_exp).getId(),
                services,
                model,
                node,
                root);
    }

    protected void parse(TransformingNodeId target, TransformingNodeId root, Services services, TransformingModel model, TransformingNode node, DefaultRootImpl rootImpl){
        rootImpl.setRoot(root);
        rootImpl.setTarget(target);
        model.getNode(target).setRoot(rootImpl);
        //System.out.println("target="+rootImpl.getTarget()+", root="+rootImpl.getRoot()+", "+Arrays.deepToString(Thread.currentThread().getStackTrace()));
    }
}
